﻿package lobby
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import lobby.events.LobbyEvent;
	import gs.TweenMax;
	import lobby.constants.ConstData;
	
	/**
	 * ...
	 * @author ... Doan Nguyen Thuy Nha Truc (Bamboo)
	 */
	public class PageControl extends MovieClip
	{
		public var firstPage:MovieClip;
		public var prePage:MovieClip;
		public var maskPage:MovieClip;
		public var nextPage:MovieClip;
		public var lastPage:MovieClip;
		public var pageList:MovieClip;
		
		private var numPage:uint;
		private var firstNum:uint = 0;
		private var lastNum:uint = 0;
		private var currentPage:uint;
		private var posx:Number;
		private var disPage:uint = 4;
		private var numShowPage:uint = 5;
		private var durationMove:Number = 0.7;
		private var typeEvent:String = "";
		
		public function PageControl()
		{
			firstPage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			firstPage.buttonMode = true;
			prePage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			prePage.buttonMode = true;
			nextPage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			nextPage.buttonMode = true;
			lastPage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			lastPage.buttonMode = true;
			posx = this.pageList.x;
		}
		
		private function mouseDownHdl(e:MouseEvent):void
		{
			outEvent(this.pageList, this.numPage);
			var event:LobbyEvent;
			if (this.typeEvent == ConstData.PAGE_CONTROL_TRANSACTION_REPORT)
			{
				event = new LobbyEvent(LobbyEvent.GET_TRANSACTION_REPORT, true);
			}
			else
			{
				event = new LobbyEvent(LobbyEvent.GET_RECENT_BET, true);
			}
			setStatusPage(this.pageList, currentPage, "out");
			if (e.currentTarget == this.firstPage && this.currentPage != 1)
			{
				if (this.lastNum > numShowPage)
				{
					TweenMax.to(this.pageList, durationMove, {onStart:onStartTween, x: this.posx, onComplete:onFinishTween});
				}
				this.firstNum = 1;
				this.lastNum = this.firstNum + disPage;
				
				this.currentPage = 1;
				event.ms = this.currentPage.toString();
				dispatchEvent(event);
			}
			else if (e.currentTarget == this.lastPage && this.currentPage != this.numPage)
			{
				if (this.firstNum < (this.numPage - disPage))
				{
					TweenMax.to(this.pageList, durationMove, {onStart:onStartTween, x: (this.posx - ((this.numPage - numShowPage) * 30)), onComplete:onFinishTween});					
				}
				this.lastNum = this.numPage;
				this.firstNum = this.lastNum - disPage;
				this.currentPage = this.numPage;
				event.ms = this.currentPage.toString();
				dispatchEvent(event);
				
			}
			else if (e.currentTarget == this.prePage)
			{
				if (currentPage > 1)
				{
					this.currentPage = this.currentPage - 1;
					event.ms = this.currentPage.toString();
					dispatchEvent(event);
					if (this.currentPage == this.firstNum)
					{
						preAnimation();
					}
				}
			}
			else if (e.currentTarget == this.nextPage)
			{
				if (currentPage < this.numPage)
				{
					this.currentPage = this.currentPage + 1;
					event.ms = this.currentPage.toString();
					dispatchEvent(event);
					if (this.currentPage == this.lastNum)
					{
						nextAnimation();
					}
				}
			}
			setStatusPage(this.pageList, currentPage, "down");
		
		}
		
		private function onStartTween():void 
		{
			firstPage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			firstPage.mouseEnabled = false;
			prePage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			prePage.mouseEnabled = false;
			nextPage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			nextPage.mouseEnabled = false;
			lastPage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			lastPage.mouseEnabled = false;
			var page:MovieClip;
			for (var i:int = 1; i <= numPage; i++)
			{
				page = MovieClip(pageList.getChildByName(i.toString()));
				page.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownPageHdl);
				page.mouseEnabled = false;
			}
		}
		
		private function onFinishTween():void 
		{
			firstPage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			firstPage.mouseEnabled = true;
			prePage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			prePage.mouseEnabled = true;
			nextPage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			nextPage.mouseEnabled = true;
			lastPage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			lastPage.mouseEnabled = true;
			var page:MovieClip;
			for (var i:int = 1; i <= numPage; i++)
			{
				page = MovieClip(pageList.getChildByName(i.toString()));
				page.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownPageHdl);
				page.mouseEnabled = true;
			}
		}
		
		private function removePages(pageList:MovieClip):void
		{
			var numberChid:int = pageList.numChildren;
			if (numberChid > 0)
			{
				for (var j:int = 0; j < numberChid; j++)
				{
					pageList.removeChildAt(0);
				}
			}
		}
		
		public function addPageToPageList(pageList:MovieClip, numPage:uint, typeEvent:String):void
		{
			removePages(pageList);
			this.pageList.x = posx;
			this.typeEvent = typeEvent;
			this.numPage = numPage;
			this.firstNum = 1;
			this.lastNum = firstNum + disPage;
			this.currentPage = 1;
			var dis:int = 30;
			var xPos:int = prePage.x + prePage.width + 3;
			for (var i:int = 1; i <= numPage; i++)
			{
				var page:MovieClip = MovieClip(new PageNum());
				page.buttonMode = true;
				page.name = i.toString();
				page.x = xPos;
				xPos += dis;
				page.txtPageTf.text = i;
				page.txtPageTf.mouseEnabled = false;
				page.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownPageHdl);
				pageList.addChild(page);
				if (i == 1)
					page.gotoAndStop("down");
			}
			pageList.tabChildren = false;
		}
		
		private function mouseDownPageHdl(e:MouseEvent):void
		{
			outEvent(this.pageList, this.numPage);
			e.currentTarget.gotoAndStop("down");
			var event:LobbyEvent;
			if (this.typeEvent == ConstData.PAGE_CONTROL_TRANSACTION_REPORT)
			{
				event = new LobbyEvent(LobbyEvent.GET_TRANSACTION_REPORT, true);
			}
			else
			{
				event = new LobbyEvent(LobbyEvent.GET_RECENT_BET, true);
			}
			event.ms = e.currentTarget.name;
			dispatchEvent(event);
			currentPage = uint(e.currentTarget.name);
			if (this.currentPage == this.lastNum && this.lastNum < this.numPage)
				nextAnimation();
			if (this.currentPage == this.firstNum && this.firstNum > 1)
				preAnimation();
		}
		
		private function outEvent(pageList:MovieClip, numPage:uint)
		{
			for (var i:int = 1; i <= numPage; i++)
			{
				var temp:DisplayObject = pageList.getChildByName(i.toString());
				MovieClip(temp).gotoAndStop("out");
			}
		}
		
		private function setStatusPage(pageList:MovieClip, numPage:uint, frame:String):void
		{
			var temp:DisplayObject = pageList.getChildByName(numPage.toString());
			MovieClip(temp).gotoAndStop(frame);
		}
		
		private function nextAnimation():void
		{
			this.lastNum += 2;
			if (this.currentPage == (this.numPage - 1))
			{
				TweenMax.to(this.pageList, durationMove, {onStart:onStartTween, x: this.pageList.x - 30, onComplete:onFinishTween});
				this.firstNum = this.numPage - disPage;
				this.lastNum = this.numPage;
			}
			else if (this.lastNum <= this.numPage)
			{
				TweenMax.to(this.pageList, durationMove, {onStart:onStartTween, x: this.pageList.x - 60, onComplete:onFinishTween});
				this.firstNum = this.lastNum - disPage;
			}
		
		}
		
		private function preAnimation():void
		{
			this.firstNum -= 2;
			if (this.currentPage == 2)
			{
				TweenMax.to(this.pageList, durationMove, {onStart:onStartTween, x: this.pageList.x + 30, onComplete:onFinishTween});
				this.firstNum = 1;
				this.lastNum = this.firstNum + disPage;
			}
			else if ((this.currentPage - 2) >= 0)
			{
				TweenMax.to(this.pageList, durationMove, {onStart:onStartTween, x: this.pageList.x + 60, onComplete:onFinishTween});
				this.lastNum = this.firstNum + disPage;
			}
		
		}
	
	}
}